//
//  UpgradeView.swift
//  Nairacup
//
//  Created by wy on 2024/10/1.
//

import UIKit

class UpgradeView: UIView {
    
    var clickUpgrade: (()->Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        createUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func createUI() {
        addSubview(aplhaView)
        addSubview(bgIV)
        bgIV.addSubview(closeBtn)
        bgIV.addSubview(titleLabel)
        bgIV.addSubview(contentLabel)
        bgIV.addSubview(upgradeBtn)
        bgIV.addSubview(cancelBtn)
    }
    
    @objc func actionClose() {
        removeFromSuperview()
    }
    
    @objc func actionUpgrade() {
        if let clickUpgrade = clickUpgrade {
            clickUpgrade()
        }
        removeFromSuperview()
    }

    lazy var aplhaView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height))
        view.backgroundColor = .black.withAlphaComponent(0.34)
        
        return view
    }()
    
    lazy var bgIV: UIImageView = {
        let iv = UIImageView(frame: CGRect(x: (Adapt.windowWidth - 268)/2, y: (Adapt.windowHeight - 366)/2, width: 268, height: 366))
        iv.image = UIImage(named: "upgrade_bg_icon")
        iv.isUserInteractionEnabled = true
        
        return iv
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: 182, width: 268, height: 22))
        label.text = "New version available"
        label.font = UIFont.systemFont(ofSize: 18)
        label.textColor = UIColor.init(hex: "#028751")
        label.textAlignment = .center
        
        return label
    }()
    
    lazy var contentLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 10, y: 226, width: 248, height: 20))
        label.text = "Please upgrade to the latest version."
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.init(hex: "#333333")
        label.textAlignment = .center
        label.numberOfLines = 2

        return label
    }()
    
    lazy var closeBtn: UIButton = {
        let btn = UIButton(frame: CGRect(x: 235, y: 40, width: 30, height: 30))
        btn.setBackgroundImage(UIImage(named: "close_icon"), for: .normal)
        btn.addTarget(self, action: #selector(actionClose), for: .touchUpInside)
        
        return btn
    }()
    
    lazy var upgradeBtn: UIButton = {
        let btn = UIButton(frame: CGRect(x: 30, y: 280, width: 207, height: 49))
        btn.setBackgroundImage(UIImage(named: "upgrade_btn_icon"), for: .normal)
        btn.addTarget(self, action: #selector(actionUpgrade), for: .touchUpInside)
        
        return btn
    }()
    
    lazy var cancelBtn: UIButton = {
        let btn = UIButton(frame: CGRect(x: 30, y: 335, width: 207, height: 20))
        btn.setTitle("Cancel", for: .normal)
        btn.setTitleColor(UIColor.init(hex: "#999999"), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.addTarget(self, action: #selector(actionClose), for: .touchUpInside)
        
        return btn
    }()

    
}
